// Mark Czubin
#include "QtBoardPreviewItem.h"
#include <QDebug>

QtBoardPreviewItem::QtBoardPreviewItem(QString label)
{
//	int spacer = 20;

	setFlag(ItemIsMovable);
	setZValue(1);
	
	image  = new QPixmap();
	pixmap = new QPixmap(100, 130);
	
	QColor bgc(255, 0, 0, 127);
	
	// clear random memory
	pixmap->fill(Qt::transparent);
	
	// let's paint now
	QPainter painter(pixmap);
	
	// load in the image, qDebug() warns the stupid developer if the path wasn't wrong
	qDebug() << image->load(":/mark/is/hot.jpg");
	
	// rescale, the evil preview image
	*image = image->scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);

	// draw preview
	painter.drawPixmap(0, 0, *image);
	
	// draw title
 	painter.setPen(Qt::white);
     	painter.setFont(QFont("SansSerif", 13));
     	painter.drawText(0, 110, 100, 20, Qt::AlignHCenter | Qt::AlignBottom, label);
     	
     	
     	//REMOVE ME
     	int abc; //that shall remind you, muhahahaha
     	drawUnselected();
}

QtBoardPreviewItem::~QtBoardPreviewItem()
{
	delete image;
	delete pixmap;
}

QRectF QtBoardPreviewItem::boundingRect() const
{
	return pixmap->rect();
}

QPainterPath QtBoardPreviewItem::shape() const
{
	QPainterPath path;
	path.addPolygon(this->boundingRect());
	return path;
}

void QtBoardPreviewItem::paint(
		QPainter *painter,
		const QStyleOptionGraphicsItem *option,
		QWidget *)
{	
	painter->drawPixmap(0, 0,  *pixmap);
}

void QtBoardPreviewItem::drawSelected()
{
	// let's paint!
	QPainter painter(pixmap);

	// draw a box around the preview
	QPen pen(Qt::green, 
		 3,
		 Qt::SolidLine, 
		 Qt::RoundCap, 
		 Qt::RoundJoin);
 	painter.setPen(Qt::white);
	painter.drawRect(0, 0, 99, 99);
	
	update();
}

void QtBoardPreviewItem::drawUnselected()
{
	// let's paint!
	QPainter painter(pixmap);

	// draw a box around the preview
	QPen pen(Qt::green, 
		 3,
		 Qt::SolidLine, 
		 Qt::RoundCap, 
		 Qt::RoundJoin);
 	painter.setPen(Qt::gray);
	painter.drawRect(0, 0, 99, 99);
	
	update();
}

void QtBoardPreviewItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	emit itemChecked(this);

	QGraphicsItem::mousePressEvent(event);
}

//void QtBoardPreviewItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
//{
//	update();
//	QGraphicsItem::mouseReleaseEvent(event);
//}
